package com.example.day15;

import android.content.Context;
import android.graphics.Bitmap;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MainActivity extends AppCompatActivity {
    ListView listV;
    MyAdapter myAdapter;
    LinearLayout linearLayout;
    List<Map<String,Object>> dataList;
    MyHandler myHandler =new MyHandler(this);
    boolean isfirst =false;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        listV=findViewById(R.id.list1);
        linearLayout = findViewById(R.id.over);
        dataList=new ArrayList<Map<String,Object>>();
        Message message = Message.obtain();
        message.what=100;
        HashMap<String,String> map =new HashMap<>();
        map.put("stage_id",1+"");
        map.put("limit",50+"");
        map.put("page",1+"");
        new WorkThread(WorkThread.STIRING,myHandler,message,"http://www.qubaobei.com/ios/cf/dish_list.php",map).start();

        //下面设置悬浮和头顶部分内容
        final View header = View.inflate(this, R.layout.list_hand_layout, null);//头部内容,会隐藏的部分
        listV.addHeaderView(header);//添加头部
        final View header2 = View.inflate(this, R.layout.list_long_layout, null);//头部内容,一直显示的部分
        listV.addHeaderView(header2);//添加头部
        listV.addFooterView(header);//添加足部
        listV.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Log.e("mmmmmmmm"+id,dataList.get(position).toString());
            }
        });

        listV.setOnScrollListener(new AbsListView.OnScrollListener() {

            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {
                Log.e("scrollState",scrollState+"");
                switch(scrollState){
                    case SCROLL_STATE_IDLE://静止状态
                        Log.e("###","静止");
                        if(isfirst){
                            Message message = Message.obtain();
                            message.what=100;
                            HashMap<String,String> map =new HashMap<>();
                            map.put("stage_id",1+"");
                            map.put("limit",50+"");
                            map.put("page",1+"");
                            new WorkThread(WorkThread.STIRING,myHandler,message,"http://www.qubaobei.com/ios/cf/dish_list.php",map).start();
                            isfirst=false;
                        }
                        break;
                    case SCROLL_STATE_TOUCH_SCROLL://滑动状态
                        Log.e("###","滑动");
                        break;
                    case SCROLL_STATE_FLING://惯性滑动状态
                        Log.e("###","惯性滑动");
                        break;
                }
            }
            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
                Log.e("####滑动","目前的第一行firstVisibleItem："+firstVisibleItem+"   visibleItemCount“"+visibleItemCount+"   总共多少行 totalItemCount："+totalItemCount);
//                if(firstVisibleItem==0){
//
//                }
                if(firstVisibleItem+visibleItemCount>=totalItemCount){
                    isfirst =true;
                }
//                if (firstVisibleItem >= 1) {
//
//                    linearLayout.setVisibility(View.VISIBLE);
//                } else {
//
//                    linearLayout.setVisibility(View.GONE);
//                }
            }
        });

    }


    private static class MyHandler extends Handler {
        private WeakReference<Context> reference;   //
        public MyHandler(Context context) {
            reference = new WeakReference<>(context);//这里传入activity的上下文
        }
        @Override
        public void handleMessage(Message msg) {
            MainActivity activity = (MainActivity) reference.get();
            super.handleMessage(msg);
            switch (msg.what) {      //判断标志位
                case 100:
                    String jsonStr = msg.obj.toString();
                    Log.e("jsonSTR",jsonStr);
                    try {
                        JSONObject json= new JSONObject(jsonStr);
                        JSONArray jsA = json.getJSONArray("data");
                        int length = jsA.length();
                        for(int i =0;i<length;i++){
                            Map<String,Object> map = new HashMap<>();
                            JSONObject jsobj = jsA.getJSONObject(i);
                            map.put("title",jsobj.getString("title"));
                            map.put("food_str",jsobj.getString("food_str"));
                            map.put("pic", null);
                            activity.dataList.add(map);
                            Message message = Message.obtain();
                            message.what=300;
                            message.arg1=i;
                            new WorkThread(WorkThread.MAP,activity.myHandler,message,jsobj.getString("pic"),null).start();
                            Log.e("jsonD",i+"");
                        }
                        activity.myAdapter  = new MyAdapter(activity,activity.dataList);
                        activity.listV.setAdapter(activity.myAdapter);
//                        activity.missHand();
                        activity.listV.setSelection(1);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    break;
                case 300:
                    if(msg.obj!=null){
                        activity.dataList.get(msg.arg1).put("pic", (Bitmap)msg.obj);
                        activity.myAdapter.notifyDataSetChanged();
                    }
                    break;
            }

        }
    }
    private void missHand(){
        Log.e("#####","缩");
        myHandler.post(new Runnable() {
            @Override
            public void run() {
                listV.smoothScrollToPosition(1);
            }
        });

    }
}
